package com.multysite.controller.admin;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.NamespaceManager;
import com.multysite.entity.admin.ApplicationMapping;
import com.multysite.model.admin.ApplicationMappingModel;
import com.multysite.util.Setting;

@SuppressWarnings("serial")
public class AddDomainServlet extends HttpServlet {

	private static final Logger log = Logger.getLogger(AddDomainServlet.class
			.getName());

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try {
			resp.setCharacterEncoding("utf-8");
			if (!NamespaceManager.get().equals(Setting.getGeneralNamespace())) {
				req.getRequestDispatcher("/admin/pages/domain-form.jsp")
						.forward(req, resp);
			} else {
				resp.getWriter().println("Invalid Url");
			}
		} catch (ServletException e) {
			e.printStackTrace();
			log.warning(e.toString());
			resp.sendError(4000,
					"Website is down for maintenance. We are sorry for the inconvenience !");
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			String myApp = "multywebsite";

			String domain = req.getParameter("domain");
			String subDomain = "";
			String isSubDomain = req.getParameter("subDomain");
			if (!NamespaceManager.get().equals(Setting.getGeneralNamespace())) {
				String currentNs = NamespaceManager.get();
				if (domain != null && domain.length() > 0) {
					try {
						if (isSubDomain != null && isSubDomain.equals("on")) {
							subDomain = domain;
							String temp = "";
							String[] splitDomain = domain.split("\\.");
							for (int i = 1; i < splitDomain.length; i++) {
								if (i == splitDomain.length - 1) {
									temp += splitDomain[i];
								} else {
									temp += splitDomain[i] + ".";
								}
							}
							domain = temp;
						}
						if (ApplicationMappingModel.getById(subDomain) != null) {
							resp.getWriter()
									.println(
											"This domain has been token. Try another or contact us");
						} else {
							ApplicationMapping mapping = new ApplicationMapping();
							mapping.setApplicationId(currentNs);
							mapping.setDomain(subDomain);
							ApplicationMappingModel.insert(
									Setting.getGeneralNamespace(), mapping);
							resp.sendRedirect("https://www.google.com/a/"
									+ domain
									+ "/ServiceLogin?service=CPanel&passive=1209600&cpbps=1&continue=https://www.google.com/a/cpanel/"
									+ domain
									+ "/AddAppEngineService?appId%3D"
									+ myApp
									+ "&followup=https://www.google.com/a/cpanel/"
									+ domain + "/AddAppEngineService?appId%3D"
									+ myApp);
						}
					} catch (Exception e) {
						e.printStackTrace();
						resp.getWriter().print("This domain has been used !");
					}

				}
			} else {
				resp.getWriter().println("Invalid Url");
			}

		} catch (Exception e) {
			e.printStackTrace();
			log.warning(e.toString());
			resp.sendError(4000,
					"Website is down for maintenance. We are sorry for the inconvenience !");
		}
	}

}
